import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;


public class Main {
    //用户名
    private static String connectUser = "root";
    //密码
    private static String connectPassword = "123456";
    //主机
    private static String connectHost = "localhost";
    //端口
    private static String connectPort = "3306";
    //数据库名
    private static String dataBaseName = "mysql";
    private static Connection connection = null;

    public static void main(String[] args) {
        //传入参数用于连接数据库
        Mysql testDataBase = new Mysql(
                connectUser,
                connectPassword,
                connectHost,
                connectPort,
                dataBaseName
        );

        //连接数据库
        connection = testDataBase.connect();

        try {
            //获取数据库语句操作对象
            Statement statement = connection.createStatement();

            //定义SQL语句
            String sql = "select user, host from user";

            //执行SQL语句
            System.out.println("SQL is running……");
            ResultSet resultSet = statement.executeQuery(sql);

            //打印查询结果
            System.out.println("print result:");
            System.out.println("--------------------");
            System.out.println("|user|host|");
            while (resultSet.next()) {//按行遍历查询结果

                //获取一行中属性'user'的值
                String user = resultSet.getString("user");

                //获取一行中属性'host'的值
                String host = resultSet.getString("host");

                //打印结果
                System.out.println("|" + user + "|" + host + "|");
            }
            System.out.println("--------------------");

            //增删改操作
            //int i = statement.executeUpdate(sql);

            //释放资源
            resultSet.close();
            statement.close();
            connection.close();

        } catch (Exception e) {
            System.out.println(e);
        }
    }
}